extends ../layout/_layout

append pageCss
    link(rel="stylesheet", href="/css/filepond.css")

append content
    main.page__container.page__main
        .page__content
            .content__container
                #form-list
                    .form-label
                        .label-desc 文章 id
                        .label-content
                            input#postid.label-input(type="text", data-post="path")

                        .label-desc 标题
                        .label-content
                            input.label-input(type="text", data-post="title")

                        .label-desc 作者
                        .label-content
                            input.label-input(type="text", value="keydone", data-post="author")

                        .label-desc 文章分类
                        .label-content
                            input.label-input(type="text", data-post="category", value="思如泉涌")

                        .label-desc 发布时间
                        .label-content
                            input.label-input(type="text", data-post="date")

                        .label-desc 内容
                        .label-content
                            textarea.label-input(type="text", data-post="content")

                        .label-desc 上传图片
                        .label-content
                             input#file-cover.filepond(type="file")

                a#post(href="javascript:;") 发布
                a#draft(href="javascript:;") 存为草稿

append pagejs
    script(type="text/javascript", src="/js/filepond.min.js")
    script.
        window.onload = function() {
            var postid = $('#postid');
            FilePond.create(document.querySelector('#file-cover'), {
                labelIdle: '拖拽或 <span class="filepond--label-action"> 浏览文件 </span>',
                labelFileWaitingForSize: '正在加载文件信息',
                labelFileSizeNotAvailable: '大小不可用',
                labelFileLoading: '正在加载',
                labelFileLoadError: '加载文件失败',
                labelFileProcessing: '正在上传',
                labelFileProcessingComplete: '已完成',
                labelFileProcessingAborted: '已取消',
                labelFileProcessingError: '上传失败',
                labelTapToCancel: '取消',
                labelTapToRetry: '重试',
                labelTapToUndo: '撤销',
                labelButtonRemoveItem: '移除',
                labelButtonAbortItemLoad: '中断',
                labelButtonRetryItemLoad: '重试',
                labelButtonAbortItemProcessing: '中断',
                labelButtonUndoItemProcessing: '撤销',
                labelButtonRetryItemProcessing: '重试',
                labelButtonProcessItem: '上传',
                name: 'filepond',
                allowRevert: false,
                allowMultiple:true,
                dropOnPage: true,
                dropValidation: true,
                onerror: function(error, file, status) {
                    console.log(error, file, status);
                }
            });

            FilePond.setOptions({
                server: {
                    url: '/api/note-upload?store=note&postid=' + postid.val(),
                },
            });

            postid.on('blur', function() {
                FilePond.removeFile();
                FilePond.setOptions({
                    server: {
                        url: '/api/note-upload?store=img&postid=' + postid.val(),
                    },
                });
            });
        }
    script.
        var post = $('#post'),
            draft = $('#draft'),
            fields = $('[data-post]'),
            data = {};

        function publish(type) {

            for(var i=0, length=fields.length; i<length; i++) {
                const item = $(fields[i]);
                var key = item.attr('data-post');
                var val = item.val();
                data[key] = val;
            }

            data.isDraft = type;

            $.ajax({
                type: 'POST',
                url: '/admin/note-save',
                dataType: 'json',
                data: data,
                success: function(res) {
                    if(res.status === 0) {
                        console.log('success');
                    }
                },
            });
        }

        post.on('click', function() {
            publish(0);
        });

        draft.on('click', function() {
            publish(1);
        });
